package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.query.SalesReporQueryObject;
import cn.wolfcode.crm.service.ISalesReporService;
import cn.wolfcode.crm.util.PermissionName;
import com.alibaba.fastjson.JSON;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: crm
 * @description:
 * @author: Mr.chen
 * @create: 2018-07-07 22:06
 **/
@Controller
@RequestMapping("salesReport")
public class SalesReporController {
    @Autowired
    private ISalesReporService salesReporService;
    /**
     * 销售报表页面
     * @return
     */
    @RequestMapping("")
    public String saleReportPage(){
        return "reportforms/sales/salesReport";
    }

    /**
     * 销售报表页面数据
     * @param qo
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    @RequiresPermissions("salesrepor:list")
    @PermissionName("销售报表数据")
    public List<Map<String, Object>> list(SalesReporQueryObject qo) {
        List<Map<String, Object>> list = salesReporService.querySaleForList(qo);
        return list;
    }

    /**
     * 销售柱状图数据
     * @return
     */
    @RequestMapping("salesbar")
    public String salesbar(SalesReporQueryObject qo, Model model){
        //分组类型, 获取到qo的分组字段, 用作副标题*/
        model.addAttribute("groupSaleByTypes",
                SalesReporQueryObject.groupSaleByTypes.get(qo.getGroupByType()));

        List<Map<String, Object>> saleChat = salesReporService.querySaleForList(qo);

        List<Object> goupByTypes = new ArrayList<>();
        List<Object> saleAmounts = new ArrayList<>();
        for (Map<String, Object> map : saleChat) {
            goupByTypes.add(map.get("groupByType"));
            saleAmounts.add(map.get("totalAmount"));
        }
        model.addAttribute("groupByType", JSON.toJSONString(goupByTypes));
        model.addAttribute("totalAmount",JSON.toJSONString(saleAmounts));

        return "reportforms/sales/salesbar";
    }

    /**
     * 销售饼状图数据
     * @return
     */
    @RequestMapping("salespie")
    public String salespie(SalesReporQueryObject qo,Model model){
        //分组类型, 获取到qo的分组字段, 用作副标题*/
        model.addAttribute("groupSaleByTypes",SalesReporQueryObject
                .groupSaleByTypes.get(qo.getGroupByType()));

        /*获取到查询出来的结果集*/
        List<Map<String, Object>> saleChart = salesReporService.querySaleForList(qo);

        List<Object> goupByTypes = new ArrayList<>();
        List<Map<String, Object>> datas = new ArrayList<>();

        /*循环遍历结果集*/
        for (Map<String, Object> map : saleChart) {
            goupByTypes.add(map.get("groupByType"));

            Map<String,Object> data = new LinkedHashMap<>();
            data.put("value", map.get("totalAmount"));
            data.put("name", map.get("groupByType"));
            datas.add(data);
        }
        model.addAttribute("goupByTypes", JSON.toJSONString(goupByTypes));
        model.addAttribute("datas",JSON.toJSONString(datas));

        return "reportforms/sales/salespie";
    }

}
